package com.lry;

import com.lry.read.ReadConsistency;
import com.lry.read.ReadOne;
import com.lry.write.WriteConsistency;
import com.lry.write.WriteOne;
import com.lry.write.WriteQuorum;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingQueue;

@Slf4j
public class NodeManager {

    private List<Node> nodes;
    private ReadConsistency readConsistency;
    private WriteConsistency writeConsistency;

    //默认读一致性级别是读一个即可，写一致性级别是写一半以上
    public NodeManager(Node... inNodes){
        this(new ReadOne(),new WriteQuorum(),inNodes);
    }

    public NodeManager(ReadConsistency readConsistency,Node... inNodes){
        this(readConsistency,new WriteQuorum(),inNodes);
    }

    public NodeManager(WriteConsistency writeConsistency,Node... inNodes){
        this(new ReadOne(),writeConsistency,inNodes);
    }

    public NodeManager(ReadConsistency readConsistency, WriteConsistency writeConsistency,Node... inNodes){
        this.nodes = new ArrayList<>();
        this.readConsistency = readConsistency;
        this.writeConsistency = writeConsistency;
        registerNodes(inNodes);

    }

    private void registerNodes(Node... inNodes){
        for (Node inNode : inNodes) {
            if(null==inNode){
                continue;
            }
            nodes.add(inNode);
        }
    }

    public String put(String key,String value){
        //判空
        return writeConsistency.put(key,value,nodes);
    }

    public NodeValue get(String key){
        //判空
        return readConsistency.get(key,nodes);
    }

}
